//
//  TopicDetailViewController.m
//  Protect -A
//
//  Created by 付剑英 on 16/6/28.
//  Copyright © 2016年 付剑英. All rights reserved.
//

#import "TopicDetailViewController.h"
#import "APPTools.h"
#import "baseTableview.h"
#import "TopicHeaderDetaillView.h"
#import "TopicDetailTableViewCell.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
@interface TopicDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, retain) baseTableview *tableView;
@property (nonatomic, retain) TopicHeaderDetaillView *viewHeader;

@property (nonatomic, retain) TopicModel *modelHeader;
@property (nonatomic, retain) NSMutableArray *arrComment;

@property (nonatomic, assign) NSInteger page;/**< 用来保存请求新数据动态值 */
@end

@implementation TopicDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.arrComment = [NSMutableArray array];
    [self dataTop];
    [self dataHandler];
    [self createHeaderView];
    [self createTableView];
    // Do any additional setup after loading the view.
}
#pragma mark - ------------ 数据处理 ---------------
- (void)dataTop
{
    
    //头部数据
    NSString *strDetail = @"http://203.80.144.212/napi/topic/detail/?locale=zh_CN&include_fields=share_links&screen_width=320&screen_height=568&platform_version=9.3.1&app_code=gandalf&platform_name=iPhone%20OS&device_platform=iPhone5%2C2&__domain=www.duitang.com&device_name=iPhone%205&app_version=6.0.6%20rv%3A156728&topic_id=240656";
    
    NSString *detailURL = [strDetail stringByReplacingOccurrencesOfString:@"240656" withString:_modelDetail.selfID.stringValue];
    
    [ APPTools   GETWithURL:detailURL par:nil success:^(id responseObject) {
        
        self.modelHeader = [[TopicModel alloc]init];
        
        NSDictionary *dic = responseObject[@"data"];
        [_modelHeader setValuesForKeysWithDictionary:dic];
        
        [self createHeaderView];
        [_tableView reloadData];
        
        [_modelHeader release];
        
        
    } filed:^(NSError *error) {
        
    }];
    
}


- (void)dataHandler
{
    
    
    //tableview数据
    NSString *strComment = @"http://203.80.144.212/napi/topic/comment/list/?locale=zh_CN&limit=0&screen_height=568&screen_width=320&start=0&platform_version=9.3.1&app_code=gandalf&device_platform=iPhone5%2C2&platform_name=iPhone%20OS&__domain=www.duitang.com&app_version=6.0.6%20rv%3A156728&topic_id=240656&device_name=iPhone%205";
    
    NSString *commentURL= [strComment stringByReplacingOccurrencesOfString:@"240656" withString:_modelDetail.selfID.stringValue];
    
    NSString *url = [commentURL stringByReplacingOccurrencesOfString:@"start=0" withString:[NSString stringWithFormat:@"start=%ld", _page]];
    
    [APPTools GETWithURL:url par:nil success:^(id responseObject) {
        
        NSArray *arrComment = responseObject[@"data"][@"object_list"];
        NSArray *arr = [TopicModel modelHandleWithArrar:arrComment];
        [_arrComment addObjectsFromArray:arr];
        
        [_tableView reloadData];
    } filed:^(NSError *error) {
        
    }];
}

#pragma mark - ------------ 头视图 ---------------
- (void)createHeaderView
{
    
    CGFloat h = [TopicDetailTableViewCell heightWithText:_modelHeader.content];
    self.viewHeader = [[TopicHeaderDetaillView alloc]init];
    _viewHeader.model = _modelHeader;
//    _viewHeader.dk_backgroundColorPicker = DKColorWithColors(GrayLight,[UIColor lightGrayColor]);
    /**< 判断 返回不同的高度  */
    if (_modelHeader.path != nil) {
        _viewHeader.frame = CGRectMake(0, 0, SCREEN_WIDTH, h + 360);
    }else{
        
        _viewHeader.frame = CGRectMake(0, 0, SCREEN_WIDTH, h + 210);
    }
    self.tableView.tableHeaderView = _viewHeader;
    
    [_viewHeader release];
    
}

#pragma mark - ------------ 创建Tableview ---------------
- (void)createTableView
{
    
    self.tableView = [[baseTableview alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //隐藏分割线
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerClass:[TopicDetailTableViewCell class] forCellReuseIdentifier:@"pool"];
    /** 夜间 */
//    _tableView.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],[UIColor grayColor]);
    [_tableView release];
    
}


#pragma mark - ------------ 返回个数 ---------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _arrComment.count;
}

#pragma mark - ------------ cell赋值 ---------------

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   TopicDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
//    cell.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
//    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_arrComment.count != 0) {
        
        TopicModel *m = _arrComment[indexPath.row];
        cell.model = m;
    }
    
    return cell;
}

#pragma mark - ------------ 返回高度 ---------------
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_arrComment.count != 0) {
        
        TopicModel *m = _arrComment[indexPath.row];
        
        CGFloat h = [TopicDetailTableViewCell heightWithText:m.content];
        
        CGFloat h1 = [TopicDetailTableViewCell heightWithText:[[m.replayUsername stringByAppendingString:@":   "] stringByAppendingString:m.replayContent]];
        
        if (m.path != nil && m.replayContent != nil) {
            
            return h + h1 + 326;
            
        }else if (m.path == nil && m.replayContent == nil){
            
            
            return h + 115;
            
        }else if (m.path != nil && m.replayContent == nil){
            
            return h + 245;
            
            
        }else if (m.path == nil && m.replayContent != nil){
            
            return h + h1 + 196;
            
            
        }
    }
    return 0;
    
    
}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
